package com.yan.controller;

import com.jfinal.core.Controller;
import com.yan.annon.RouterMapping;
import com.yan.bean.Diary;
import com.yan.bean.PageBean;
import com.yan.bean.User;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * Created by yanl_ on 2018/1/7.
 */
@RouterMapping(url = "/diary", viewPath = "/")
public class DiaryController extends Controller {


    public List<Diary> diaryList(PageBean pageBean, Diary s_diary) throws Exception {


        return null;
    }


    public void querydiary() {
        //     String diaryId = getPara("diaryId");
        int diaryId = getParaToInt("diaryId");
        Diary diary = Diary.dao.queryDiary(diaryId);
        setSessionAttr("diary", diary);
        setSessionAttr("mainPage", "diary/diaryShow.jsp");
        redirect("/main.jsp");
    }


    public void diarydelete() {
        int diaryId = getParaToInt("diaryId");
        boolean flag = Diary.dao.deleteDiary(diaryId);
        redirect("/main");

    }


    public void diaryedit() {

        String diaryId = getPara("diaryId");
        if (!StringUtils.isEmpty(diaryId)) {
            Diary diary = Diary.dao.queryDiary(Integer.valueOf(diaryId));
            setSessionAttr("diary", diary);
        }
        setSessionAttr("mainPage", "diary/diarySave.jsp");
        redirect("/main.jsp");

    }


    public void diarysave() {

        Diary diary = getBean(Diary.class, "");
//        String title=request.getParameter("title");
//        String content=request.getParameter("content");
//        String typeId=request.getParameter("typeId");
//        String diaryId=request.getParameter("diaryId");
        boolean flag;
        if (diary.getDiaryId() == null) {
            flag = Diary.dao.savediary(diary);
        } else {
            flag = Diary.dao.editdiary(diary);
        }


        if (flag) {
            redirect("/main");
        } else {
            setSessionAttr("diary", diary);
            setSessionAttr("mainPage", "diary/diarySave.jsp");
            setAttr("error", "操作失败");
            redirect("/main.jsp");
        }

//        if (saveNums > 0) {
//            request.getRequestDispatcher("main?all=true").forward(request, response);
//        } else {
//            request.setAttribute("diary", diary);
//            request.setAttribute("error", "����ʧ��");
//            request.setAttribute("mainPage", "diary/diarySave.jsp");
//            request.getRequestDispatcher("mainTemp.jsp").forward(request, response);
//        }

    }

}
